﻿#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::WidgetClass())
{
    ui->setupUi(this);

    connect(ui->pushButton_3, &QPushButton::clicked, this, &Widget::hadleClicked);
    connect(this, &Widget::mySignal, this, &Widget::handleMySignal);
    QPushButton* button = new QPushButton(this);
    button->setText("按钮");
    connect(button, &QPushButton::clicked, this, [=](){
        qDebug() << "lambda 被执行了!";
        button->move(100, 200);
    });
}

Widget::~Widget()
{
    delete ui;
}

void Widget::hadleClicked()
{
    this->setWindowTitle("修改窗口项目的标题");
}

void Widget::hadleClicked2()
{
    this->setWindowTitle("修改窗口项目的标题2");
}

void Widget::handleMySignal(const QString& text)
{
    this->setWindowTitle(text);
}

void Widget::on_pushButton_clicked()
{
    emit mySignal("把标题设置为标题1", "");
}

void Widget::on_pushButton_2_clicked()
{
    emit mySignal("把标题设置为标题2", "");
}

void Widget::on_pushButton_4_clicked()
{
    // 断开槽函数连接
    disconnect(ui->pushButton_3, &QPushButton::clicked, this, &Widget::hadleClicked);
    // 重新绑定信号槽
    connect(ui->pushButton_3, &QPushButton::clicked, this, &Widget::hadleClicked2);
}

void Widget::on_pushButton_enabled_test_clicked()
{
    qDebug() << "执行槽函数";
}

void Widget::on_pushButton_enabled_clicked()
{
    bool enable = ui->pushButton_enabled_test->isEnabled();
    if (enable)
        ui->pushButton_enabled_test->setEnabled(false);
    else
        ui->pushButton_enabled_test->setEnabled(true);
}
